<?php

class DataField {
	private $TYPES=array(
		"tinyint" =>"com.mysql.yado.fields.parsers.TinyIntParser",
		"int"     =>"com.mysql.yado.fields.parsers.IntParser",
		"varchar" =>"com.mysql.yado.fields.parsers.VarCharParser",
		"date"    =>"com.mysql.yado.fields.parsers.DateParser",
		"datetime"=>"com.mysql.yado.fields.parsers.DateTimeParser",
		"text"    =>"com.mysql.yado.fields.parsers.TextParser"
	);
	public function __construct(ITableDescriptor $table,$params){
		$this->_table=$table;
		foreach ($params as $param=>$value) {
			switch ($param) {
				case "Field":
					$this->_name=$value;
				break;
				case "Type":
					$this->setType($value);
				break;
				case "Null":
					$this->_isNull=$value=="YES";
				break;
				case "Key":
					$this->_key=$value;
				break;
				case "Default":
					$this->_defaultValue = $value;
				break;
				case "Extra":
					$this->_extra = $value;
				break;
			}
		}
		
	}
	/**
	 * Enter description here...
	 *
	 * @var ITable
	 */
	private $_table;
	public function getTable(){
		return $this->_table;
	}
	public function getDescriptor(){
		return $this->_table->getDescriptor();
	}
	private $_value;
	
	/*public function __construct($value){
		$this->parseVariable($value); 
		$this->_value=$value;
	}*/
	
	public function setValue($value){
		$this->parseVariable($value); 
		$this->_value = $value;
	}
	public function getValue(){
		return $this->_value;
	}
	public $_defaultValue;
	public function getDefaultValue(){
		return $this->_defaultValue;
	}
	private $_name;
	public function getName(){
		return $this->_name;
	}
	
	private $_type;
	public function getType(){
		return $this->_type;
	}
	protected function setType($type){
		$type=$this->parseType($type);
		foreach ($this->TYPES as $_type=>$parser){
			if ($type==$_type){
				try{
					import($parser);
					$class = explode(".",$parser);
					$class = $class[sizeof($class)-1];
					$this->_parser = new $class;
					//var_dump($this->_parser);
				}catch (ClassNotFoundException $e){
					// TODO create data field exception and throw it.
					echo $e->getMessage()."<br />";
				}
			}
		}
	}
	private $_parser;
	
	protected function parseType($type){
		$expl = explode(" ",$type);
		$type = $expl[0];
		$this->_unsigned = isset($expl[1]);
		if (strpos("(",$type)!=-1){
			$expl = explode(" ",str_replace("("," ",str_replace(")","",$type)));
			$type=$expl[0];
			if (isset($expl[1])){
				$this->_length = (int)$expl[1];
			}
			//var_dump((int)$length);
		}
		return $type;
	}
	
	private $_isNull=false;
	public function isNull(){
		return $this->_isNull;
	}
	private $_key;
	public function getKey(){
		return $this->_key;
	}
	public function isPrimary(){
		return $this->_key=="PRI";
	}
	
	private $_extra;
	public function getExtra(){
		return $this->_extra;
	}
	private $_zerofill=false;
	public function getZerofill(){
		return $this->_zerofill;
	}
	private $_unsigned=true;
	public function getUnsigned(){
		return $this->_unsigned;
	}
	private $_length;
	public function getLength(){
		return $_length;
	}
	
	protected function parseVariable($value){
		$this->_parser->parseValue($this,$value);
	}
	
	/**
	 * Enter description here...
	 * @return array;
	 */
	public function getAll(){
		
	}
	
	public function __toString(){
		echo "VALUE";
		var_dump($this->_value);
		return $this->_value;
	}
}

?>